//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System;
using Microsoft.Win32;

namespace Microsoft.TridentWordAddIn.Setup.CustomActions
{
    public class RegisterOffice2007AddIn
    {   
        private const string AddInName = "Trident.WordAddIn";
        private const string VstoName = "Microsoft.TridentWordAddIn.AddIn.vsto";        
        private const string AppRegKeyPath = @"Software\Microsoft\Office\14.0\User Settings\Word AddIn";
        private const string AddInPath = @"Software\Microsoft\Office\Word\AddIns";        
        private const string AddInDeletePath = "Delete" + "\\" + AddInPath;
        private const string AddInCreatePath = "Create" + "\\" + AddInPath;

        /// <summary>
        /// Increments the count.
        /// </summary>
        public static void IncrementCount()
        {
            RegistryKey appKey = Registry.LocalMachine.OpenSubKey(AppRegKeyPath, true);        
            object count = appKey.GetValue("Count", -1);
            if (Convert.ToInt32(count) == -1)
            {
                appKey.SetValue("Count", 1);            
            }
            else
            {
                appKey.SetValue("Count", Convert.ToInt32(count) + 1);
            }

            appKey.Close();
        }
        
        /// <summary>
        /// Removes the trident word add in entry.
        /// </summary>
        public static void RemoveTridentWordAddInEntry()
        {
            string path = AppRegKeyPath + "\\" + AddInCreatePath;
            RegistryKey appKey = Registry.LocalMachine.OpenSubKey(path, true);
            if (appKey != null)
            {
                RegistryKey tridentWordAddInKey = appKey.OpenSubKey(AddInName, true);
                if (tridentWordAddInKey != null)
                {
                    tridentWordAddInKey.Close();
                    appKey.DeleteSubKeyTree(AddInName);
                }

                appKey.Close();
            }
        }       

        /// <summary>
        /// Registers the delete instruction.
        /// </summary>
        public static void AddTridentWordAddInDeleteInstruction()
        {            
            string path = AddInDeletePath + "\\" + AddInName;
            RegistryKey appKey = Registry.LocalMachine.OpenSubKey(AppRegKeyPath, true);
            appKey.CreateSubKey(path);            
            appKey.Close();
        }     

        /// <summary>
        /// Removes the trident word add in delete instruction.
        /// </summary>
        public static void RemoveTridentWordAddInDeleteInstruction()
        {
            string path = AppRegKeyPath + "\\" + AddInDeletePath;
            RegistryKey appKey = Registry.LocalMachine.OpenSubKey(path, true);
            if (appKey != null)
            {
                RegistryKey tridentWordAddInKey = appKey.OpenSubKey(AddInName, true);
                if (tridentWordAddInKey != null)
                {
                    tridentWordAddInKey.Close();
                    appKey.DeleteSubKeyTree(AddInName);
                }

                appKey.Close();
            }
        }      

        /// <summary>
        /// Removes the registry entries for single user.
        /// </summary>
        public static void RemoveRegistryEntriesForSingleUser()
        {            
            RegistryKey appKey = Registry.CurrentUser.OpenSubKey(AddInPath, true);
            if (appKey != null)
            {
                RegistryKey tridentWordAddInKey = appKey.OpenSubKey(AddInName, true);
                if (tridentWordAddInKey != null)
                {
                    tridentWordAddInKey.Close();
                    appKey.DeleteSubKeyTree(AddInName);
                }

                appKey.Close();
            }
        }

        /// <summary>
        /// Creates the base registry entries.
        /// </summary>
        /// <param name="allUsers">All users.</param>
        /// <param name="targetDirectory">The target directory.</param>
        public static void CreateAppRegistryEntries(string allUsers, string targetDirectory)
        {
            RegistryKey addInAppKey;
            string regKeyPathAddInAllUsers = AppRegKeyPath + "\\" + AddInCreatePath + "\\" + AddInName;            
            string regKeyPathAddInSingleUser = AddInPath + "\\" + AddInName;
            string vstoFilePath = targetDirectory + VstoName + "|vstolocal";
            if (allUsers != null && allUsers.CompareTo("1") == 0)
            {
                addInAppKey = Registry.LocalMachine.CreateSubKey(regKeyPathAddInAllUsers);
                addInAppKey.Close();
                addInAppKey = Registry.LocalMachine.OpenSubKey(regKeyPathAddInAllUsers, true);
            }
            else
            {
                addInAppKey = Registry.CurrentUser.CreateSubKey(regKeyPathAddInSingleUser);
                addInAppKey.Close();
                addInAppKey = Registry.CurrentUser.OpenSubKey(regKeyPathAddInSingleUser, true);
            }

            addInAppKey.SetValue("Description", "Trident Word Add-In", RegistryValueKind.String);
            addInAppKey.SetValue("FriendlyName", "Trident Word Add-In", RegistryValueKind.String);
            addInAppKey.SetValue("LoadBehavior", 3, RegistryValueKind.DWord);
            addInAppKey.SetValue("Manifest", vstoFilePath, RegistryValueKind.String);
        }

        /// <summary>
        /// Removes the app registry entries.
        /// </summary>
        /// <param name="allUsers">All users.</param>
        public static void RemoveAppRegistryEntries()
        {            
            bool allUsers = GetInstallType();
            if (allUsers)
            {
                IncrementCount();
                RemoveTridentWordAddInEntry();                
                AddTridentWordAddInDeleteInstruction();                
            }
            else
            {
                RemoveRegistryEntriesForSingleUser();                
            }           
        }

        /// <summary>
        /// Gets the type of the install.
        /// </summary>
        /// <returns></returns>
        public static bool GetInstallType()
        {
            string path = AppRegKeyPath + "\\" + AddInCreatePath + "\\" + AddInName;
            RegistryKey appKey = Registry.LocalMachine.OpenSubKey(path);
            if (appKey != null)
            {
                return true;
            }

            return false;
        }        
    }
}
